// pages/category/category.js
import {Category} from 'category-model';
var category = new Category();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden:false,
        currentMenuIndex:0,
        rightArr:[],
        categoryTypeArr:[],
        transClassArr:['tanslate0','tanslate1','tanslate2','tanslate3','tanslate4','tanslate5'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this._onload();
    },
    _onload(callback){
        var that = this;
        category.getCategoryType(res=>{
            that.categoryTypeArr = res;
            that.setData({
                categoryTypeArr:res,
                loadingHidden:true
            },function (res) {
                console.log(res);
                that.getDetaiByCetegory(0);
            });
        });
    },
    getDetaiByCetegory(index){
        var that = this,
            cetegoryID = this.categoryTypeArr[index].id;
        category.getCategoryDetailById(cetegoryID,function (res) {
            var dataObj= {
                procucts: res,
                topImgUrl: this.categoryTypeArr[index].img.url,
                title: this.categoryTypeArr[index].name
            };
            that.data.rightArr[cetegoryID] = dataObj;
        })
    },
    //切换分类
    changeCategory:function(event){
        var index=category.getDataSet(event,'index'),
            id=category.getDataSet(event,'id')//获取data-set
        this.setData({
            currentMenuIndex:index
        });
        //如果数据是第一次请求
        if(!this.isLoadedData(index)) {
            var that=this;
            this.getProductsByCategory(id, (data)=> {
                that.setData(that.getDataObjForBind(index,data));
            });
        }
    },
    isLoadedData:function(index){
        if(this.data['categoryInfo'+index]){
            return true;
        }
        return false;
    },
    getDataObjForBind:function(index,data){
        var obj={},
            arr=[0,1,2,3,4,5],
            baseData=this.data.categoryTypeArr[index];
        for(var item in arr){
            if(item==arr[index]) {
                obj['categoryInfo' + item]={
                    products:data,
                    topImgUrl:baseData.img.url,
                    title:baseData.name
                };

                return obj;
            }
        }
    },
    /*跳转到商品详情*/
    onProductsItemTap: function (event) {
        var id = category.getDataSet(event, 'id');
        wx.navigateTo({
            url: '../product/product?id=' + id
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this._loadData(()=>{
            wx.stopPullDownRefresh()
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})